home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15163 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pointer to Functions and Calls thereof??
  5. Date: 17 Apr 1996 15:53:54 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4l346i$iph@sparcserver.lrz-muenchen.de>
  9. References: <Dq01Ft.Dqn@latcs1.lat.oz.au>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. woelkerl@lion.cs.latrobe.edu.au (Eric Woelkerling) writes:
  13.  
  14. >    Hi!    Can anybody fill me in as to how to get a pointer to a function
  15. >and then use this to do the call??  I have a situation where a particular function
  16. >will be selected from a set of functions and used throughout my entire code,
  17. >but the function selected will depend on the runtime selection by thge user..
  18.  
  19. >so far.. I have got as far as..
  20.  
  21. >    void    func1(void){naughty bits}
  22. >    void    func2(void){naughty bits}
  23.  
  24. >    void    main(void)
  25.  
  26. I will not comment on this!
  27.  
  28. >    {    void    *a[1];
  29.  
  30. There is some use for arrays of size 1, e.g. for "simulating" call
  31. by reference in a "user friendly" way, but storing _two_ pointers
  32. to anything is _not_ a good use for an array of 1 void pointer. 
  33.  
  34. >        a[1]=func1;
  35.  
  36. A void pointer can hold all data pointers, but this does not include
  37. pointers to functions. So why do you insist on using void pointers 
  38. to store pointers to functions?
  39.  
  40. The only element of "a" that can be accessed given the current
  41. definition of "a" is "a[0]".
  42.  
  43. >        a[2]=func2;
  44.  
  45. If you want to access "a[2]", define "a" as "T a[3]". Arrays are
  46. zero based in C.
  47.  
  48. >        (*a[1])();
  49.  
  50. "a" is an array of pointers to void. "a[1]" is a pointer to void,
  51. and dereferencing it is therefore not possible. If a void pointer
  52. can hold a pointer to a function in your environment, something
  53. like
  54.  
  55.    ((void (*)(void))a[1])();
  56.  
  57. will work. If you prefer "dereferencing" function pointers when 
  58. you call them, this will become
  59.  
  60.   (*((void (*)(void))a[1]))();
  61.  
  62. but remember, both are not portable to machines where void pointers
  63. cannot hold pointers to functions (and PCs using the right memory
  64. model are such machines). Why don't you simply define a "proper" 
  65. function pointer type, as in
  66.  
  67.   typedef void (*PROC)(void);
  68.  
  69. declare "a" as an array of PROC, as in
  70.  
  71.   PROC a[2];
  72.  
  73. and use it in a "simple" way, as in
  74.  
  75.   a[1]();
  76.  
  77. Kurt
  78. --
  79. | Kurt Watzka                             Phone : +49-89-2180-6254
  80. | watzka@stat.uni-muenchen.de
  81.  
  82.